import axios, { AxiosRequestConfig } from 'axios'

import type * as TDetail from '../BatchAddOrigRouteDrawer/interface'
import type * as TList from '../interface'
import { normalizeReqData } from '../utils'
import './__mock__'

/** 获取原始路由列表 */
export const apiGetOrigRouteList = async (
  req: TList.IOrigRouteListReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<TList.IOrigRouteListRes> => {
  const data = normalizeReqData(req)
  const waybillNos = (data.waybillNos?.trim() || undefined)?.split(/\s*\n+\s*/)
  return axios.post(`/tmsapi/original_route/list`, { ...data, waybillNos }, { signal }).then(res => ({
    ...res.$data,
    records: res.$data.records || [],
  }))
}

/** 批量新增原始路由 */
export const apiBatchAddOrigRoute = async (
  req: TDetail.IBatchAddOrigRouteReqItem[],
): Promise<TDetail.IBatchAddOrigRouteRes> => {
  return axios.post(`/tmsapi/original_route/batchAdd`, req).then(res => res.$data)
}

/** 删除原始路由 */
export const apiDelOrigRoute = async (originalRouteId: number): Promise<void> => {
  await axios.post(`/tmsapi/original_route/del`, { originalRouteId })
}
